import router from '@/router'
import store from '@/store'
router.beforeEach(async(to, from, next) => {
  if (store.getters.token) {
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.state.user.userinfo.userId) {
        await store.dispatch('user/getuserinfo')
        const permission = await store.dispatch('permission/getmenus', store.getters.menus)
        router.addRoutes(permission)
        next(to.path)
      } else {
        next()
      }
    }
  } else {
    const withelist = ['/login', '/404']
    if (withelist.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
    }
  }
})
